package com.moon.game.command.guest.lair;

import java.util.List;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.AnimalMG;
import com.moon.game.resource.model.Animal;
import com.moon.game.service.PlayerService;
import com.moon.game.util.GDateUtils;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.Tower;

/**
 * @description 迷幻(留住)珍兽
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-14 下午11:22:14
 */
public class PsychedelicAnimalCmd extends BaseCmd {

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		PlayerVo player = req.getPlayer();
		List<Tower> towers = player.getTowerList();
		if (towers == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "进入孵化塔出错，你当前没有孵化塔！"));
			return;
		}
		Integer towerId = Integer.parseInt(ps[0]);
		if (towerId == null) {
			doResponsePsError(req);
			return;
		}
		boolean hasTheLair = false;
		Tower theTower = null;
		for (Tower tower : towers) {
			if (tower.getTowerId() == towerId.intValue()) {
				tower.setLastShowTime(GDateUtils.now());
				hasTheLair = true;
				theTower = tower;
				break;
			}
		}
		//没有指定孵化塔
		if (!hasTheLair) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "你当前没有指定的孵化塔！"));
			return;
		}
		
		boolean minusStatu = PlayerService.minusGold(req.getSid(), req.getPlayerId(), Constants.PSYCHEDELIC_ANIMAL_PRICE);
		if (!minusStatu) {
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_GOLD, "你的金币不足" + Constants.PSYCHEDELIC_ANIMAL_PRICE + "，我不能给你迷幻珍兽，强行留住它！"));
			return;
		}
		
		int addTime = 0;
		Animal animal = AnimalMG.instance.getAnimal(theTower.getAnimalId());
		switch (animal.getHierarchy()) {
			case 0:
				theTower.setAnimalCalledTime(theTower.getAnimalCalledTime() + Constants.PSYCHEDELIC_COMMON_ANIMAL_TIME);
				addTime = Constants.PSYCHEDELIC_COMMON_ANIMAL_TIME;
				break;
			case 1:
				theTower.setAnimalCalledTime(theTower.getAnimalCalledTime() + Constants.PSYCHEDELIC_IMMORTAL_ANIMAL_TIME);
				addTime = Constants.PSYCHEDELIC_IMMORTAL_ANIMAL_TIME;
				break;
			case 2:
				theTower.setAnimalCalledTime(theTower.getAnimalCalledTime() + Constants.PSYCHEDELIC_GOD_ANIMAL_TIME);
				addTime = Constants.PSYCHEDELIC_GOD_ANIMAL_TIME;
				break;
			default:
				break;
		}
		req.setVO("add_time", addTime);
		req.setVO("animal_name", animal.getName());
		req.setVO("tower_id", towerId);
		PlayerService.doUpdatePlayer(req.getSid(), req.getPlayerId(), player);
		response(req, req.getClientResponse());
	}
}
